package test3;

import test3.GUI.HighlightedPanel;
import test3.GUI.UnderlinedTextArea;
import test3.parser.TokenManager;
import test3.parser.Token;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 2, 2007
 * Time: 5:45:33 PM
 * Revision: 0
 */
public class WndTest extends JFrame implements ActionListener
{
    private UnderlinedTextArea hPanel;

    private SimpleAttributeSet sty_normal;
    private SimpleAttributeSet sty_keyword;
    private SimpleAttributeSet sty_comment;
    private SimpleAttributeSet sty_string;
    private SimpleAttributeSet sty_number;

    private Timer t;

    public WndTest()
    {
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        getContentPane().setLayout(new BorderLayout());

        hPanel = new UnderlinedTextArea();
        hPanel.setText("/* acesta este un test ***/\n");
        getContentPane().add(hPanel, BorderLayout.CENTER);

        JButton btn = new JButton("Parse");
        btn.addActionListener(this);
        getContentPane().add(btn, BorderLayout.SOUTH);
        setVisible(true);

//         Style style = textPane.addStyle("Red", null);
//    StyleConstants.setForeground(style, Color.red);
//
//    // Inherits from "Red"; makes text red and underlined
//    style = textPane.addStyle("Red Underline", style);
//    StyleConstants.setUnderline(style, true);

        sty_keyword = new SimpleAttributeSet();
        StyleConstants.setForeground(sty_keyword, Color.blue);
        StyleConstants.setBold(sty_keyword, true);

        sty_comment = new SimpleAttributeSet();
        StyleConstants.setForeground(sty_comment, new java.awt.Color(200, 200, 200));
        StyleConstants.setItalic(sty_comment, true);

        sty_string = new SimpleAttributeSet();
        StyleConstants.setForeground(sty_string, new java.awt.Color(160, 80, 0));

        sty_number = new SimpleAttributeSet();
        StyleConstants.setForeground(sty_number, new java.awt.Color(255, 0, 255));

        sty_normal = new SimpleAttributeSet();
        StyleConstants.setForeground(sty_normal, Color.black);


        t = new Timer(1000, this);
        t.setInitialDelay(1000);

        hPanel.getStyledDocument().addDocumentListener(new DocumentListener()
        {
            public void changedUpdate(DocumentEvent e)
            {
                t.stop();
                t.start();
            }

            public void insertUpdate(DocumentEvent e)
            {
                t.stop();
                t.start();
            }

            public void removeUpdate(DocumentEvent e)
            {
                t.stop();
                t.start();
            }
        });
    }

    public static void main(String[] args)
    {
        new WndTest();
    }

    public void actionPerformed(ActionEvent e)
    {
        String text = hPanel.getText();
        TokenManager tm = new TokenManager(text.toCharArray());

        StyledDocument doc = hPanel.getStyledDocument();

        Token t;

        tm.initSearch();

        int lastPos = 0;
        do
        {
            t = tm.getNextToken(true);
            SimpleAttributeSet style = null;

            if (t.getType() == Token.TYPE_END_OF_FILE)
                style = sty_normal;
            if (t.getType() == Token.TYPE_STR_COMMENT)
                style = sty_comment;

            if (t.getType() == Token.TYPE_STR_NUMBER)
                style = sty_number;

            if (t.getType() == Token.TYPE_STR_IDENTIFIER)
            {
                style = sty_keyword;
            }


            if (style != null)
            {
                doc.setCharacterAttributes(lastPos, t.getStartPos() - lastPos, sty_normal, true);
                lastPos = t.getEndPos();

                doc.setCharacterAttributes(t.getStartPos(), t.getEndPos() - t.getStartPos(), style, true);
            }


        }
        while (t.getType() != Token.TYPE_END_OF_FILE);
    }
}
